﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.OA.Biz
{
    public class ProductBiz
    {
        /// <summary>
        /// 获取所有产品信息
        /// </summary>
        /// <returns></returns>
        public static List<Product> GetProductList()
        {
            try
            {
                var productList = new Product().FindAll();
                return productList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 分页获取产品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Product> GetProductListByPage(int page, int pageSize, string key, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var productList = from c in ctx.Product
                                      where c.Status != (int)ItemStatus.Delete && (c.ProductName.Contains(key) || c.Summary.Contains(key))
                                      orderby c.CreateTime descending
                                      select c;
                    count = productList.Count();
                    var data = productList
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Product().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 获取所有产品信息
        /// </summary>
        /// <returns></returns>
        public static List<Product> GetProductList(string key)
        {
            try
            {
                var productList = new Product().FindAll(c => c.ProductName.Contains(key));
                return productList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 增加产品
        /// </summary>
        /// <returns></returns>
        public static bool ProductAdd(string productName, string summary, string rateSummary)
        {
            try
            {
                var product = new Product() { 
                    CreateTime = DateTime.Now,
                    CreateUser = System.Web.HttpContext.Current.User.Identity.Name,
                    ProductName = productName, 
                    RateSummary = rateSummary,
                    Summary = summary,
                    Status = ItemStatus.Enable
                };
                return product.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID获取产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static Product GetProductByID(int productId)
        {
            try
            {
                return new Product().Find(productId.ToString());
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 根据状态获取产品
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<Product> GetProductByStatus(int status)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var productList = (from c in ctx.Product where c.Status == status orderby c.ID select c);
                    var data = productList.ToList().Select(p => new Product().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="product"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static bool ProductEdit(int productId, string productName, string summary, string rateSummary)
        {
            try
            {
                var product = new Product().Find(productId.ToString());
                if (product != null)
                {
                    product.CreateTime = DateTime.Now;
                    product.CreateUser = System.Web.HttpContext.Current.User.Identity.Name;
                    product.ProductName = productName;
                    product.RateSummary = rateSummary;
                    product.Summary = summary;
                    return product.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }

        public static bool ProductStatusChange(int productId, int destinationStatus)
        {
            try
            {
                var product = new Product().Find(productId.ToString());
                if (product != null)
                {
                    product.Status = (ItemStatus)destinationStatus;
                    return product.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }
    }
}
